define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        moment = require('moment'),
        helper = require('js/apps/report/common');

    var ProfitView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch'
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.ProfitReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        transformData: function(list){
            _.each(list, function(data) {
                data.realProfit = data.recharge_price - data.withdraw_price;
                if(data.recharge_price) data.realProfit_ratio = data.realProfit / data.recharge_price;
                else data.realProfit_ratio = 0;
                data.realPay_price = data.pay_price - data.refund_price - data.coupon_use;
                data.realWin_price = data.win_price + data.bonus_price;
                data.realGain = data.realPay_price - data.realWin_price;
                if(data.realPay_price) data.realGain_ratio = data.realGain / data.realPay_price;
                else data.realGain_ratio = 0;
                data.real_dwc_payprice = data.game_bet_amount;
                data.real_dwc_winprice = data.game_win_amount;
                data.real_dwc_gain = data.game_bet_amount - data.game_win_amount;
                if(data.real_dwc_payprice) data.real_dwc_gain_ratio = data.real_dwc_gain / data.real_dwc_payprice;
                else data.real_dwc_gain_ratio = 0;
                data.real_metis_payprice = data.metis_bet_amount;
                data.real_metis_gain = data.metis_profit || 0;
                console.log(data.real_metis_gain)
                data.real_metis_winprice = data.metis_bet_amount - data.metis_profit;
                if(data.real_metis_payprice) data.real_metis_gain_ratio = data.real_metis_gain / data.real_metis_payprice;
                else data.real_metis_gain_ratio = 0;
                data.totalGain = data.realGain + data.real_dwc_gain + data.real_metis_gain;
                if(!data.coupon_use) data.coupon_use = 0;
                if(!data.system_recharge_price) data.system_recharge_price = 0;
            });
            var sum = _.chain(list).values().reduce(function (c, p){
                return _.mapObject(c, function (value, key) {
                   return p[key] + value;
                });
              }).value();
            /*
            if(sum) {
                if (sum.recharge_price) sum.realProfit_ratio = sum.realProfit / sum.recharge_price;
                else sum.realProfit_ratio = 0;
                if (sum.realPay_price) sum.realGain_ratio = sum.realGain / sum.realPay_price;
                else sum.realGain_ratio = 0;
                if (sum.real_dwc_payprice) sum.real_dwc_gain_ratio = sum.real_dwc_gain / sum.real_dwc_payprice;
                else sum.real_dwc_gain_ratio = 0;
                this.model.sum = sum;
            }*/

            return list;
        },
        renderWithData: function () {
            this.model.list = this.transformData(this.model.list);
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
                sum: this.model.sum
            }));
            helper.loadDatePicker(this.options);
            var x = _.pluck(this.model.list, '_id'),
                recharge_price = _.pluck(this.model.list, 'recharge_price'),
                profit = _.pluck(this.model.list, 'realProfit'),
                profitRate = _.pluck(this.model.list, 'realProfit_ratio'),
                totalGain = _.pluck(this.model.list, 'totalGain');
            $('#chart_recharge').highcharts({
                chart: {
                    zoomType: 'xy'
                },
                title: {
                    text: '付费走势曲线'
                },
                xAxis: [{
                    categories: x,
                    crosshair: true
                }],
                yAxis: [{
                    labels: {
                        style: {
                            color: 'blue',
                        }
                    },
                    title: {
                        text: '金额',
                        style:{
                            color: 'blue'
                        }
                    }
                },{
                    title: {
                        text: '利润率',
                    },
                    opposite: true
                }],
                tooltip: {
                    shared: true
                },
                series:[{
                    name: '金额',
                    type: 'spline',
                    data: recharge_price,
                    tooltip: {
                        pointFormat: '<br/>{series.name}: <b>{point.y: .2f}</b>'
                    }
                },{
                    name: '利润率',
                    type: 'spline',
                    data: profitRate,
                    yAxis: 1,
                    tooltip: {
                        pointFormat: '<br/>{series.name}: <b>{point.y: .2f}</b>'
                    }
                }]
            });

            $('#chart_profit').highcharts({
                chart: {
                    zoomType: 'xy'
                },
                title: {
                    text: '利润走势曲线'
                },
                xAxis: [{
                    categories: x,
                    crosshair: true
                }],
                yAxis: [{ //利润值
                    labels: {
                        style: {
                            color: 'blue',
                        }
                    },
                    title: {
                        text: '金额',
                        style: {
                            color: 'blue'
                        }
                    },
                }, { //比例
                    title: {
                        text: '利润率',
                    },

                    opposite: true
                }],
                tooltip: {
                    shared: true
                },
                series: [{
                    name: '金额',
                    type: 'spline',
                    data: profit,
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.y: .2f}</b>'
                    }
                }, {
                    name: '利润率',
                    type: 'spline',
                    data: profitRate,
                    yAxis: 1,
                    tooltip: {
                        pointFormat: '<br/>{series.name}: <b>{point.y: .2f}</b>'
                    },
                }]
            });


            $('#chart_gain').highcharts({
                chart: {
                    zoomType: 'xy'
                },
                title: {
                    text: '总抽水走势曲线'
                },
                xAxis: [{
                    categories: x,
                    crosshair: true
                }],
                yAxis: [{ //利润值
                    labels: {
                        style: {
                            color: 'blue',
                        }
                    },
                    title: {
                        text: '金额',
                        style: {
                            color: 'blue'
                        }
                    },
                }, { //比例
                    title: {
                        text: '利润率',
                    },

                    opposite: true
                }],
                tooltip: {
                    shared: true
                },
                series: [{
                    name: '金额',
                    type: 'spline',
                    data: totalGain,
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.y: .2f}</b>'
                    }
                }, {
                    name: '利润率',
                    type: 'spline',
                    data: profitRate ,
                    yAxis: 1,
                    tooltip: {
                        pointFormat: '<br/>{series.name}: <b>{point.y: .2f}</b>'
                    },
                }]
            });

            return this;
        },
        load: function () {
            this.model.fetch({
                reset: true,
                data: {
                    date_range: this.options.dateRange.join(','),
                },
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            })
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Profit.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                week_ago = moment().subtract(7, 'day').format('YYYY-MM-DD'),
                dateRange = [week_ago, today],
                view = new ProfitView({
                    dateRange: dateRange
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});